const api = require('../../network/api')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        hotList: [],
        searchKey: '',
        searchs: []
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.init()
    },
    async init() {
        const param = {
            page: Math.floor(Math.random() * (30 - 1)) + 1,
            pageSize: 9
        }
        const res = await api.getCartoons(param)
        if (res.code == 200) {
            this.setData({
                hotList: res.data.data
            })
        }
    },

    onChange(e) {
        this.setData({
            searchKey: e.detail,
        });
        // console.log(e.detail);
        if (!e.detail) {
            this.setData({
                searchs: []
            })
        }
    },
    async onSearch() {
        const param = {
            works: this.data.searchKey
        }
        if (!this.data.searchKey) {
            return
        }
        const res = await api.queryCartoon(param)
        console.log(res, 'res');
        if (res.code == 200) {
            this.setData({
                searchs: res.data
            })
        }

    },
    onDetail(e) {
        // console.log(e.currentTarget.dataset.id);
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/detail/index?id=${id}`,
        })

    }
})